package com.commerceerp.system.controller;


import com.alibaba.fastjson.JSONArray;
import com.commerceerp.system.entity.Constant;
import com.commerceerp.system.entity.PorderInfo;
import com.commerceerp.system.entity.SupplierInfo;
import com.commerceerp.system.service.PurchaseOrderService;
import com.commerceerp.system.service.SupplierService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.RdPage;
import com.commerceerp.system.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "供应商接口", value = "供应商接口")
@Scope("prototype")
@RestController
@EnableAutoConfiguration
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value = "查询采购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "supplierId", value = "供应商id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierName", value = "供应商名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierContact", value = "联系人", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierPhone", value = "联系方式", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierEmail", value = "email", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierQq", value = "QQ", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierHomepage", value = "主页", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierCompanyAddress", value = "公司地址", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supplierRemark", value = "备注", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "(1:增加,2:编辑,3:删除,4:查询所有供应商,5:查找某一供应商信息)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "记录数", required = false, dataType = "Integer"),
    })
    @RequestMapping(value = "/handleSupplierInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult handleSupplierInfo( @RequestParam(value = "supplierId", required = false) String supplierId,
                                         @RequestParam(value = "supplierName", required = false) String supplierName,
                                         @RequestParam(value = "supplierContact", required = false) String supplierContact,
                                         @RequestParam(value = "supplierPhone", required = false) String supplierPhone,
                                         @RequestParam(value = "supplierEmail", required = false) String supplierEmail,
                                         @RequestParam(value = "supplierQq", required = false) String supplierQq,
                                         @RequestParam(value = "supplierHomepage", required = false) String supplierHomepage,
                                         @RequestParam(value = "supplierCompanyAddress", required = false) String supplierCompanyAddress,
                                         @RequestParam(value = "supplierRemark", required = false) String supplierRemark,
                                         @RequestParam(value = "type", required = false) String type,
                                         @RequestParam(value = "current", required = false) Integer current,
                                         @RequestParam(value = "pageSize", required = false) Integer pageSize) throws IOException {

        ApiResult result = null;

        if("1".equals(type)){
            SupplierInfo supplierInfo = new SupplierInfo();
            supplierInfo.setSupplierId(StringUtils.getUUID());
            supplierInfo.setSupplierCompanyAddress(supplierCompanyAddress);
            supplierInfo.setSupplierContact(supplierContact);
            supplierInfo.setSupplierCreateTime(DateUtil.getYMDHMSDate());
            supplierInfo.setSupplierEmail(supplierEmail);
            supplierInfo.setSupplierHomepage(supplierHomepage);
            supplierInfo.setSupplierName(supplierName);
            supplierInfo.setSupplierPhone(supplierPhone);
            supplierInfo.setSupplierQq(supplierQq);
            supplierInfo.setSupplierRemark(supplierRemark);

            Integer success = supplierService.saveNewSupplierInfo(supplierInfo);

            if(success == 1){
                result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
            }else{
                result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
            }

        } else if("2".equals(type)){

            SupplierInfo supplierInfo = new SupplierInfo();
            supplierInfo.setSupplierId(StringUtils.getUUID());
            supplierInfo.setSupplierCompanyAddress(supplierCompanyAddress);
            supplierInfo.setSupplierContact(supplierContact);
            supplierInfo.setSupplierCreateTime(DateUtil.getYMDHMSDate());
            supplierInfo.setSupplierEmail(supplierEmail);
            supplierInfo.setSupplierHomepage(supplierHomepage);
            supplierInfo.setSupplierName(supplierName);
            supplierInfo.setSupplierPhone(supplierPhone);
            supplierInfo.setSupplierQq(supplierQq);
            supplierInfo.setSupplierRemark(supplierRemark);
            supplierInfo.setSupplierId(supplierId);
            supplierInfo.setSupplierUpdTime(DateUtil.getYMDHMSDate());

            Integer success = supplierService.updSupplierInfo(supplierInfo);

            if(success == 1){
                result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
            }else{
                result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
            }

        } else if("3".equals(type)){

            Integer success = supplierService.delSupplierInfo(supplierId);

            if(success == 1){
                result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"成功",null,null);
            }else{
                result = new ApiResult(Constant.FAIL_CODE_VALUE,"失败",null,null);
            }
        }else if("4".equals(type)){

            PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));

            SupplierInfo supplierInfo = new SupplierInfo();
            supplierInfo.setSupplierContact(supplierContact);
            supplierInfo.setSupplierName(supplierName);
            supplierInfo.setSupplierPhone(supplierPhone);

            List<Map> list = supplierService.getAllSupplierInfo(supplierInfo);

            //获取分页信息
            PageInfo<Map> pageInfo = new PageInfo<Map>(list);
            RdPage rdPage = RdPage.getPageInfo(pageInfo, String.valueOf(current), String.valueOf(pageSize));

            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",list,rdPage);

        }else if("5".equals(type)){

            SupplierInfo supplierInfo = supplierService.getSupplierInfoById(supplierId);

            result = new ApiResult(Constant.SUCCEED_CODE_VALUE,"查询成功",supplierInfo,null);

        }

        return result;
    }


}
